package com.lihui;

/**
 * 功能描述
 *
 * @author wileda
 * @date 2022/09/23  19:31
 */
class Calculator<E extends Comparable<E>> {

    public E findMaxValue1 (E[] arrays) {
        if (arrays.length == 0) {
            throw new RuntimeException("数据为空");
        }
        E max = arrays[0];
        for (int i = 0; i < arrays.length; i++) {
            if (max.compareTo(arrays[i]) < 0) {
                max = arrays[i];
            }
        }
        return max;
    }
    public int findMaxValue (int[] arrays) {
        if (arrays.length == 0) {
            throw new RuntimeException("数据为空");
        }
        int max = arrays[0];
        for (int i = 0; i < arrays.length; i++) {
            if (max < arrays[i]) {
                max = arrays[i];
            }
        }
        return max;
    }
}

class Calculator1 {
    // 泛型方法的定义
    // 实例泛型方法
    public <E extends Comparable<E>> E findMaxValue (E[] arrays) {
        if (arrays.length == 0) {
            throw new RuntimeException("数据为空");
        }
        E max = arrays[0];
        for (int i = 0; i < arrays.length; i++) {
            if (max.compareTo(arrays[i]) < 0) {
                max = arrays[i];
            }
        }
        return max;
    }

    // 静态泛型方法
    public static  <E extends Comparable<E>> E findMaxValue1 (E[] arrays) {
        if (arrays.length == 0) {
            throw new RuntimeException("数据为空");
        }
        E max = arrays[0];
        for (int i = 0; i < arrays.length; i++) {
            if (max.compareTo(arrays[i]) < 0) {
                max = arrays[i];
            }
        }
        return max;
    }
}

public class Demo05 {
    public static void main(String[] args) {
        String[] arrays = {"hello", "world", "aaa", "bbb"};
        String max = Calculator1.findMaxValue1(arrays);
        System.out.println(max);
    }

    public static void main4(String[] args) {
        String[] arrays = {"hello", "world", "aaa", "bbb"};
        Calculator1 calculator1 = new Calculator1();
        String max = calculator1.findMaxValue(arrays);
        System.out.println(max);
    }

    public static void main3(String[] args) {
        String[] arrays = {"hello", "world", "aaa", "bbb"};
        Calculator<String> calculator = new Calculator<>();
        String max = calculator.findMaxValue1(arrays);
        System.out.println(max);
    }

    public static void main2(String[] args) {
        Integer[] arrays = {3, 56, 78, 22, 108, 356};
        Calculator<Integer> calculator = new Calculator<>();
        int max = calculator.findMaxValue1(arrays);
        System.out.println(max);
    }

    public static void main1(String[] args) {
        int[] arrays = {3, 56, 78, 22, 108, 356};
        Calculator calculator = new Calculator();
        int max = calculator.findMaxValue(arrays);
        System.out.println(max);
    }
}

